//package com.caiyi.financial.nirvana.sms.scheduled;
//
//import com.alibaba.fastjson.JSON;
//import com.caiyi.financial.nirvana.sms.enums.SmsGatewayStatusEnum;
//import com.caiyi.financial.nirvana.sms.enums.SmsUserTypeEnum;
//import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
//import com.caiyi.financial.nirvana.sms.jedis.JedisService;
//import com.caiyi.financial.nirvana.sms.queue.AlarmSendQueueEntity;
//import com.caiyi.financial.nirvana.sms.service.SmsGatewayService;
//import com.caiyi.financial.nirvana.sms.service.SmsMsgService;
//import com.caiyi.financial.nirvana.sms.util.Constant;
//import com.caiyi.financial.nirvana.sms.vo.SmsRateVo;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Map;
//
//@Component
//public class FailedRateTask {
//
//    private static final Logger logger = LoggerFactory.getLogger(FailedRateTask.class);
//    private static final String GATEWAY_ID = "1";
//    @Value("${spring.profiles.active}")
//    private String env;
//    @Autowired
//    private SmsMsgService smsMsgService;
//    @Autowired
//    private SmsGatewayService smsGatewayService;
//
//    /**
//     * 前10分钟失败率监控
//     * 每10分钟执行一次
//     */
//    @Scheduled(fixedRate = Constant.TEN_MINUTES_OF_SECOND * 1000, initialDelay = Constant.TEN_MINUTES_OF_SECOND * 1000)
//    public void failedRate() {
//        try {
//            List<SmsRateVo> list = smsMsgService.getMsgFailedRate();
//            for (SmsRateVo smsRateVo : list) {
//                String gatewayName = smsRateVo.getName();
//                String gatewayId = smsRateVo.getGatewayId();
//                Integer gatewayType = smsRateVo.getGatewayType();
//                Double msgCount = smsRateVo.getTotalCount();
//                Double failedCount = smsRateVo.getFailedCount() == null ?
//                        0d : smsRateVo.getFailedCount();
//                Double failedRate = Math.floor(failedCount / msgCount * 100);
//                logger.info("FailedRateTask 失败率监控 gatewayName:{}, failedCount : {}, msgCount : {}, failedRate : {}", gatewayName, failedCount, msgCount, failedRate);
//                if (msgCount > 0 && failedRate > (100 - Constant.SUCCESS_RATE)) {
//                    AlarmSendQueueEntity entity = new AlarmSendQueueEntity();
//                    entity.setPhone(Constant.PHONE);
//                    String content = String.format(Constant.SMS_FAILED_RATE_CONTENT, gatewayName, failedRate + "%", (100 - Constant.SUCCESS_RATE) + "%");
//                    entity.setContent(content);
//                    logger.info("FailedRateTask 失败率监控报警 : {}, content : {}, size : {}", Constant.PHONE, content, msgCount);
//                    if (msgCount < 10 || SmsUserTypeEnum.MARKETING.index == gatewayType) {
//                        continue;
//                    }
//                    if (env.equals(Constant.ENV_PRO) && GATEWAY_ID.equals(gatewayId)) {
//                        logger.info("FailedRateTask 测试通道失败率监控日志 : {}, content : {}, size : {}", Constant.PHONE, content, msgCount);
//                    } else {
//                        JedisService.lpush(JedisKey.SMS_QUEUE_ALARM_MSG_SEND, JSON.toJSONString(entity));
//                        //发现通道失败率大于20%，禁用此通道
//                        smsGatewayService.updateStatus(Integer.parseInt(gatewayId), SmsGatewayStatusEnum.DISABLE.index);
//                    }
//                } else {
//                    logger.info("FailedRateTask 失败率监控运行完成,数据正常. 通道名称：{}，总数：{}，失败数：{}，失败率：{}", gatewayName, msgCount, failedCount, failedRate);
//                }
//            }
//            // 一个月移动发送条数，存redis
//            int ydCount = smsMsgService.getCountAndGetWayId(Constant.YD_GATEWAY_ID);
//            int ltCount = smsMsgService.getCountAndGetWayId(Constant.LT_GATEWAY_ID);
//            String key = String.format(JedisKey.SMS_ID_GATEWAY_BALANCE, Constant.YD_GATEWAY_ID);
//            JedisService.set(key, String.valueOf(ydCount));
//            key = String.format(JedisKey.SMS_ID_GATEWAY_BALANCE, Constant.LT_GATEWAY_ID);
//            JedisService.set(key, String.valueOf(ltCount));
//            logger.info("yd count : {}, lt count : {}", ydCount, ltCount);
//        } catch (Exception e) {
//            logger.error("FailedRateTask 失败率监控 run error", e);
//        }
//
//    }
//
//}
